package com.tocici.service.impl;

import com.tocici.domain.vo.UserStatVo;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.tocici.domain.bo.UserBo;
import com.tocici.domain.vo.UserVo;
import com.tocici.domain.User;
import com.tocici.mapper.UserMapper;
import com.tocici.service.IUserService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户Service业务层处理
 *
 * @author zeratul
 * @date 2025-03-02
 */
@RequiredArgsConstructor
@Service
public class UserServiceImpl implements IUserService {

    private final UserMapper baseMapper;

    /**
     * 查询用户
     *
     * @param userId 主键
     * @return 用户
     */
    @Override
    public UserVo queryById(Long userId){
        return baseMapper.selectVoById(userId);
    }

    /**
     * 分页查询用户列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 用户分页列表
     */
    @Override
    public TableDataInfo<UserVo> queryPageList(UserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<User> lqw = buildQueryWrapper(bo);
        Page<UserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的用户列表
     *
     * @param bo 查询条件
     * @return 用户列表
     */
    @Override
    public List<UserVo> queryList(UserBo bo) {
        LambdaQueryWrapper<User> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<User> buildQueryWrapper(UserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(User::getUserId);
        lqw.like(StringUtils.isNotBlank(bo.getUsername()), User::getUsername, bo.getUsername());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), User::getPhone, bo.getPhone());
        lqw.eq(bo.getAvatarUrl() != null, User::getAvatarUrl, bo.getAvatarUrl());
        lqw.like(StringUtils.isNotBlank(bo.getNickname()), User::getNickname, bo.getNickname());
        lqw.eq(bo.getGender() != null, User::getGender, bo.getGender());
        lqw.eq(StringUtils.isNotBlank(bo.getInfo()), User::getInfo, bo.getInfo());
        lqw.eq(bo.getIsDeleted() != null, User::getIsDeleted, bo.getIsDeleted());
        lqw.eq(bo.getPassword() != null,User::getPassword,bo.getPassword());
        lqw.eq(bo.getArea() != null,User::getArea,bo.getArea());
        return lqw;
    }

    /**
     * 新增用户
     *
     * @param bo 用户
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(UserBo bo) {
        User add = MapstructUtils.convert(bo, User.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setUserId(add.getUserId());
        }
        return flag;
    }

    /**
     * 修改用户
     *
     * @param bo 用户
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(UserBo bo) {
        User update = MapstructUtils.convert(bo, User.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(User entity){
        //TODO 做一些数据校验,如唯一约束
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.ne(entity.getUserId() != null,User::getUserId,entity.getUserId());
        lqw.eq(entity.getPhone() != null,User::getPhone,entity.getPhone());
        List<User> users = this.baseMapper.selectList(lqw);
        if (users.size() == 1){
            throw new ServiceException("手机号已存在");
        }
    }

    /**
     * 校验并批量删除用户信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public List<UserStatVo> getUserStat() {
        return this.baseMapper.getUserStat();

    }

    @Override
    public List<UserStatVo> getUserInc() {
        return this.baseMapper.getUserInc();
    }
}
